/*
 ============================================================================
 Name		: ARCheckersMob.cpp
 Author	  : 
 Copyright   : Your copyright notice
 Description : Main application class
 ============================================================================
 */

// INCLUDE FILES
#include <eikstart.h>
#include "ARCheckersMobApplication.h"
#include "ARCheckersMob.h"


LOCAL_C CApaApplication* NewApplication()
	{
	return new CARCheckersMobApplication;
	}

GLDEF_C TInt E32Main()
	{
	return EikStart::RunApplication(NewApplication);
	}

CARCheckersMob::CARCheckersMob()
	{
	// TODO Auto-generated constructor stub

	}

CARCheckersMob::~CARCheckersMob()
	{
	// TODO Auto-generated destructor stub
	}

CARCheckersMob* CARCheckersMob::NewL( TUint aWidth, TUint aHeight){

	CARCheckersMob* self = new (ELeave) CARCheckersMob( aWidth, aHeight );
    CleanupStack::PushL( self );
    self->ConstructL();
    CleanupStack::Pop();

    return self;
}

CARCheckersMob::CARCheckersMob( TUint aWidth, TUint aHeight ) : 
    iScreenWidth( aWidth ),
    iScreenHeight( aHeight )
    {
    }

void CARCheckersMob::ConstructL( void )
    {
    }

void CARCheckersMob::AppInit( void )
    {
    // Set the screen background color. 
    glClearColor( 0.9f, 0.9f, 0.9f, 1.f );

    // Enable back face culling. 
    glEnable( GL_CULL_FACE  );

    // Initialize viewport and projection. 
    glViewport( 0, 0, iScreenWidth, iScreenHeight );
    glMatrixMode( GL_PROJECTION );
    glFrustumf( -1.f, 1.f, -1.f, 1.f, 3.f, 1000.f );
    glMatrixMode( GL_MODELVIEW );

    // Enable vertex arrays. 
    glEnableClientState( GL_VERTEX_ARRAY );

    
    
    iBoard = new CBoard(-2 ,-2, 4);
    
    // Set array pointers. 
    //glVertexPointer( 3, GL_BYTE, 0, vertices );

    // Enable color arrays. 
    //glEnableClientState( GL_COLOR_ARRAY );

    // Set color pointers. 
    //glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colors );

    // Set the initial shading mode 
    glShadeModel( GL_FLAT ); 

    // Do not use perspective correction 
    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );

    }

void CARCheckersMob::AppExit( void )
    {
    }

void CARCheckersMob::AppCycle( TInt iFrame )
    {
    const GLint cameraDistance = 100;

    glClear( GL_COLOR_BUFFER_BIT );

    /* Animate and draw box */
    glLoadIdentity();
    glTranslatex( 0 , 0 , -cameraDistance << 16 );
    glRotatex( -60 << 16, 1 << 16,    0   ,    0    );
    //glRotatex( iFrame << 15,    0   , 1 << 16,    0    );
    glRotatex( iFrame << 14,    0   ,    0   , 1 << 16 );
    Draw( 15.f, 15.f, 15.f );
    }

void CARCheckersMob::Draw( GLfloat aSizeX, GLfloat aSizeY, GLfloat aSizeZ )
    {
    glScalef( aSizeX, aSizeY, aSizeZ );
    
    
    //glColor4f(0.f, 0.f, 0.f, 1.0f);
    iBoard->Draw();
    
    /*glColor4f(1.f, 0.f, 0.f, 0.0f);
    
    for(int i = 0; i < 8; i++){
        for(int j = 0; j < 4; j++){
            DrawRectXY(-2.0 + j*1 + 0.5*(i%2),  -2.0+0.5*i,    0.5);
        }
    }*/
    

    //glFlush();
    
    }

void CARCheckersMob::DrawRectXY(GLfloat x, GLfloat y, GLfloat l)
    {
        GLfloat square[4 * 3] =
        {
            x,   y,   0.0,
            x+l, y,   0.0,
            x,   y+l, 0.0,
            x+l, y+l, 0.0
        };

        glVertexPointer(3, GL_FLOAT, 0, square);
        glPushMatrix();
            glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
        glPopMatrix();
    }
